import { defineConfig, CommonServerOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
import dotenv, { DotenvParseOutput } from 'dotenv'
import fs from 'fs'
// https://vitejs.dev/config/
// export default defineConfig({
//   base: '/dang',
//   plugins: [vue()],
// })
export default defineConfig((mode) => {
  const curEnv = `.env.${mode.mode}`
  let serve: CommonServerOptions = {}
  const envData = fs.readFileSync(curEnv)
  const envMap: DotenvParseOutput = dotenv.parse(envData)

  if (mode.mode === 'development') {
    serve = {
      host: envMap.VITE_HOST,
      port: envMap.VITE_PORT,
      proxy: {
        [envMap.VITE_BASE_URL]: {
          target: envMap.VITE_PROXY_DOMAIN
        }
      }
    }
  }

  if (mode.mode === 'production') {
    serve = {
      port: envMap.VITE_PORT,
      host: envMap.VITE_HOST
    }
  }
  return {
    base: '/dang',
    plugins: [vue()],
  }
})